﻿package DevilCat {
	
	import flash.display.MovieClip;	
	import flash.events.MouseEvent;
	import DevilCat.PokemonObject;	
	public class PokemonInfor extends MovieClip {
		
		public var pokemon:PokemonObject;
		public var replacePokemon:PokemonObject;
		public function PokemonInfor(_pokemon:PokemonObject) {
			pokemon=_pokemon;
			AddEventShowInfor();
		}
		//----------------------------------------
		public function AddEventShowInfor(){
			ReloadText();
			LoadMovesPanel();			
			this.addEventListener(MouseEvent.MOUSE_OVER,ShowInforEvent);
			this.addEventListener(MouseEvent.MOUSE_OUT,HideInforEvent);	
			PokemonInforPanel.visible=false;		
		}
		private function ShowInforEvent(e:MouseEvent) {
			PokemonInforPanel.visible=true;
		}
		private function HideInforEvent(e:MouseEvent) {
			PokemonInforPanel.visible=false;
		}
		private function LoadMovesPanel() {
			for (var i:int=1; i<5; i++) {
				PokemonInforPanel["Move"+i].TNumber.text=pokemon["Move"+i].PP+"MP";
				PokemonInforPanel["Move"+i].TName.text=pokemon["Move"+i].Name;
			}
			PokemonInforPanel.Ability.text=pokemon.Ability;
			PokemonInforPanel.Stats.text=pokemon.Attack+"-"+pokemon.Defense+"-"+pokemon.SpAtk+"-"+pokemon.SpDef+"-"+pokemon.Speed;			
			PokemonInforPanel.visible=false;
		}
		//----------------------------------------
		public function AddEventReplacePokemon(_replacePokemon:PokemonObject){
			replacePokemon=_replacePokemon;
			this.addEventListener(MouseEvent.CLICK, ClickReplacePokemonEvent);			
		}			
		private function ClickReplacePokemonEvent(evt:MouseEvent) {
			var targetPos:Number=evt.target.pokemon.GetMyPositionInList();				
			if(targetPos>=0){
				replacePokemon.upid=SQLData.pokemonList[targetPos].upid
				SQLData.pokemonList[targetPos]=replacePokemon;
				SQLData.UpdatePokemonSQL(SQLData.pokemonList[targetPos]);
				this.parent.addChild(new FloatingText("THẢ!",310,155,2));
			}
		}
		//----------------------------------------
		public function AddEventSwitchPokemon(){
			this.addEventListener(MouseEvent.CLICK, ClickSwitchPokemonEvent);			
		}
		private function ClickSwitchPokemonEvent(evt:MouseEvent) {
			if (pokemon.CurrentHP>0) {				
				var myPos:Number=pokemon.GetMyPositionInList();
				var temPokemon:PokemonObject;
				temPokemon=SQLData.pokemonList[myPos];
				SQLData.pokemonList[myPos]=SQLData.pokemonList[0];				
				SQLData.pokemonList[0]=temPokemon;
				SQLData.mainClip.loadPokemonList();
				var Query:String="UPDATE twalk_user SET leadpkm='"+SQLData.pokemonList[0].upid+"'  WHERE uid='"+SQLData.uid+"'";
				SQLData.SendQuery(Query);
			}
		}
		public function ReloadText() {
			if (pokemon.Status!="None") {
				txtName.text=pokemon.Name+" "+pokemon.Status+" "+pokemon.upid;
			} else {
				txtName.text=pokemon.Name;
			}
			txtHP.text=String("HP:"+pokemon.CurrentHP);
		}
	}
	
}
